<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-28 19:51:46
 * @LastEditTime: 2023-11-30 11:47:52
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    title="新建设备维护"
    :width="740"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <div style='max-height: 500px;overflow: auto'>
      <a-spin :spinning='loading'>

        <a-form :form='form' v-bind='formLayout'>
          <a-form-item label='名称'>
            <a-input placeholder='请输入名称'
                     v-decorator="['name',{ rules: [
                       { required: true, message: '请输入名称!' },
                        { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}
                       ] }]" />
          </a-form-item>
          <a-form-item label='系统名称'>
            <a-select  allowClear placeholder='请选择'  v-decorator="['systemId']">
              <a-select-option :value='item.id' :key='item.id' v-for='item in roleList'>{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label='设备名称'>
            <a-input placeholder='请输入设备名称' v-decorator="['equipmentName',{
              rules:[ { required: true, message: '请输入设备名称!' }]
            }]" />
          </a-form-item>
          <a-form-item label='设备编号'>
            <a-input placeholder='请输入设备编号' v-decorator="['equipmentNo',{
              rules:[ { required: true, message: '请输入设备编号!' }]
            }]" />
          </a-form-item>
          <a-form-item label='安装位置'>
            <a-input placeholder='请输入安装位置' v-decorator="['installLocation',{
              rules:[ { required: true, message: '请输入安装位置!' }]
            }]" />
          </a-form-item>
          <a-form-item label='维护时间'>
            <a-date-picker style='width: 100%;' placeholder='请选择运维时间' v-decorator="['maintainTime',{
              rules:[ { required: true, message: '请输入运维时间!' }]
            }]" valueFormat='YYYY-MM-DD' />
          </a-form-item>
          <a-form-item label='维护人'>
            <a-input placeholder='请输入运维人' v-decorator="['maintainer',
            {
              rules:[ { required: true, message: '请输入运维人!' }]
            }]" />
          </a-form-item>
          <a-form-item label='维护人电话'>
            <a-input placeholder='请输入维护人电话'
                     v-decorator="['maintainerPhoneNo',{rules: [{ required: true, message: '请输入联系电话' }, { pattern: phonePattern,  message: '请输入合法电话'}], validateTrigger: 'blur'}]" />
          </a-form-item>
        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { namePattern, phonePattern } from '@/reges'
import {findDataDicInfoByType} from '@/api/dataDicInfo'
import TagSelectOption from '@/components/TagSelect/TagSelectOption'
// 表单字段
const fields = ['maintainerPhoneNo', 'maintainTime', 'installLocation','equipmentNo','equipmentName','systemId','name','maintainer']

export default {
  components: { TagSelectOption },
  props: {
    visible: {
      type: Boolean,
      required: true
    },

    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      namePattern,
      form: this.$form.createForm(this),
      queryParam:{},
      phonePattern,
      roleList:[],
      systemId:''
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
    this.getdictionaries()
  },
  methods: {
    getdictionaries(){
      findDataDicInfoByType(2).then(res=>{
        this.roleList = res
      })
    },
    validatorPhone(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入设备名称'))
      } else {
        if (!/^\S*$/.test(value)) {
          callback(new Error('请勿输入空格等特殊字符!'))
        } else {
          callback()
        }
      }
    },
    validaNumber(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入设备编号'))
      } else {
        if (!/^[A-Za-z0-9]+$/.test(value)) {
          callback(new Error('请输入字母加数字组合!'))
        } else {
          callback()
        }
      }
    }
  },
}
</script>
<style lang='less' scoped>
.ant-modal-body{
  height: 700px;
  overflow: auto;
}
</style>
